package com.yanyilu.qiubite.jpa;

import com.yanyilu.qiubite.entity.Story;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

public interface StoryJpa extends JpaRepository<Story,Integer> {
    @Query(value = "select story_id from story where spot_id = :storyId",nativeQuery = true)
    List<Integer> findStoryBySpotId(@Param("storyId")Integer spotId);

    @Query(value = "select * from story where spot_id = :spotId and if(:storyName is not null,story_name like concat('%',:storyName,'%'),1=1)",nativeQuery = true)
    Page<Story> findByStoryIds(@Param("storyName") String storyName, Pageable pageable, @Param("spotId") Integer spotId);

    @Query(value ="select s.story_id from story s left join admin_user au on au.spot_id = s.spot_id where au.id = :userId",nativeQuery = true)
    List<Integer> findStoryIdsByUserId(@Param("userId")Integer userid);

    @Modifying
    @Query(value = "delete from story where spot_id = :spotId",nativeQuery = true)
    void deleteBySpotId(@Param("spotId")Integer spotId);
}
